package ch02.test;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class test1 {
    public static void main(String[] args) throws Exception {
        //获取DOM解析器的工厂实例
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        //从Dom工厂获取Dom的实例
        DocumentBuilder db = dbf.newDocumentBuilder();
        //获取xml的位置
        Document doc = db.parse("src/ch02/test/test1.xml");
        //需要获取的信息
        NodeList branch = doc.getElementsByTagName("Brand");
        //循环获取
        for (int i = 0; i < branch.getLength(); i++) {
            //获取第i个信息
            Node bran = branch.item(i);
            //获取第i个的属性
            Element element = (Element) bran;

            String att = element.getAttribute("name");
            //获取他的子元素
            NodeList types = element.getChildNodes();
            for (int j = 0; j < types.getLength(); j++) {
                Element typeElement = ((Element) types.item(j));
                String type=typeElement.getAttribute("name");
                System.out.println("手机："+att+type);
            }
        }
    }
}
//<!--        <?xml version="1.0" encoding="UTF-8" standalone="no"?>--><!--<PhoneInfo>--><!--<Brand name="华为"><Type name="P90"/></Brand>--><!--<Brand name="苹果"><Type name="iPhone Z"/><Type name="iPhone ZL"/><Type name="money 50000"/></Brand>--><!--</PhoneInfo>-->